package com.cloud.lan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.lan.api.dto.req.LanBadgeUserRecordReq;
import com.cloud.lan.dao.LanBadgeUserRecordMapper;
import com.cloud.lan.dao.LanUserMapper;
import com.cloud.lan.entity.base.BaseResponse;
import com.cloud.lan.entity.model.dataobject.LanBadgeUserRecordDO;
import com.cloud.lan.entity.model.dataobject.LanUserDO;
import com.cloud.lan.service.ILanBadgeUserRecordService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * Created by zhouquan on 2022/5/19.
 * 徽章用户记录服务.增删改查
 */
@Slf4j
@Service
public class LanBadgeUserRecordServiceImpl implements ILanBadgeUserRecordService {

    @Resource
    private LanBadgeUserRecordMapper lanBadgeUserRecordMapper;
    @Resource
    private LanUserMapper lanUserMapper;

    @Override
    public BaseResponse<Object> manageList(LanBadgeUserRecordReq req) {
        log.info("徽章用户记录.查询列表:{}", req);
        List<Long> userIdList = queryLanUserIds(req);
        if(ObjectUtil.isEmpty(userIdList)){
            if(StringUtils.isNotBlank(req.getUserPhone())
                    || StringUtils.isNotBlank(req.getUserName())
                    || StringUtils.isNotBlank(req.getWxName())){
                userIdList.add(99988877766655L);
            }
        }

        LambdaQueryWrapper<LanBadgeUserRecordDO> qw = new LambdaQueryWrapper<>();
        if(ObjectUtil.isNotEmpty(userIdList)){
            qw.in(LanBadgeUserRecordDO::getUserId,userIdList);
        }
        qw.orderByDesc(LanBadgeUserRecordDO::getId);
        Page<LanBadgeUserRecordDO> page = new Page<>(req.getPageIndex(), req.getPageSize());
        IPage<LanBadgeUserRecordDO> pageList = lanBadgeUserRecordMapper.selectPage(page, qw);
        if(Objects.nonNull(pageList) && ObjectUtil.isNotEmpty(pageList.getRecords())){
            pageList.getRecords().stream().forEach(rd->{
                LanUserDO lanUserDO = queryUserByUserId(rd.getUserId());
                if(Objects.nonNull(lanUserDO) && StringUtils.isNotBlank(lanUserDO.getUserPhone())){
                    rd.setUserPhone(lanUserDO.getUserPhone());
                }
            });
        }
        return BaseResponse.success(pageList);
    }

    @Override
    public BaseResponse<Object> add(LanBadgeUserRecordReq req) {
        log.info("徽章用户记录.新增:{}", req);
        LanBadgeUserRecordDO exitDO = queryLanBadgeUserRecord(req);
        if(Objects.nonNull(exitDO)){
            return BaseResponse.success();
        }
        LanBadgeUserRecordDO lanBadgeUserRecordDO = new LanBadgeUserRecordDO();
        BeanUtil.copyProperties(req, lanBadgeUserRecordDO);
        Date nowD = new Date();
        lanBadgeUserRecordDO.setCreateTime(nowD);
        lanBadgeUserRecordDO.setUpdateTime(nowD);
        lanBadgeUserRecordMapper.insert(lanBadgeUserRecordDO);
        return BaseResponse.success();
    }

    @Override
    public BaseResponse<Object> delete(Long id) {
        log.info("徽章用户记录.删除:{}", id);
        lanBadgeUserRecordMapper.deleteById(id);
        return BaseResponse.success();
    }

    @Override
    public BaseResponse<Object> update(LanBadgeUserRecordReq req) {
        log.info("徽章用户记录.更新:{}", req);
        LanBadgeUserRecordDO exitDO = queryLanBadgeUserRecord(req);
        if(Objects.nonNull(exitDO)){
            return BaseResponse.success();
        }
        LanBadgeUserRecordDO lanBadgeUserRecordDO = new LanBadgeUserRecordDO();
        BeanUtil.copyProperties(req, lanBadgeUserRecordDO);
        Date nowD = new Date();
        lanBadgeUserRecordDO.setUpdateTime(nowD);
        lanBadgeUserRecordMapper.updateById(lanBadgeUserRecordDO);
        return BaseResponse.success();
    }

    private LanBadgeUserRecordDO queryLanBadgeUserRecord(LanBadgeUserRecordReq req){
        if(Objects.nonNull(req.getUserId()) && req.getUserId()>0
                && Objects.nonNull(req.getBadgeId()) && req.getBadgeId()>0){
            LambdaQueryWrapper<LanBadgeUserRecordDO> qw = new LambdaQueryWrapper<>();
            qw.eq(LanBadgeUserRecordDO::getUserId,req.getUserId());
            qw.eq(LanBadgeUserRecordDO::getBadgeId,req.getBadgeId());
            return lanBadgeUserRecordMapper.selectOne(qw);
        }
        return null;
    }

    private List<Long> queryLanUserIds(LanBadgeUserRecordReq req){
        List<Long> userIds = new ArrayList<>();
        if(Objects.nonNull(req.getUserId()) && req.getUserId()>0){
            userIds.add(req.getUserId());
            return userIds;
        }

        if(StringUtils.isNotBlank(req.getUserPhone())
                || StringUtils.isNotBlank(req.getUserName())
                || StringUtils.isNotBlank(req.getWxName())){
            LambdaQueryWrapper<LanUserDO> userQw = new LambdaQueryWrapper<>();
            if(StringUtils.isNotBlank(req.getUserPhone())){
                userQw.eq(LanUserDO::getUserPhone,req.getUserPhone());
            }
            if(StringUtils.isNotBlank(req.getUserName())){
                userQw.eq(LanUserDO::getUserName,req.getUserName());
            }
            if(StringUtils.isNotBlank(req.getWxName())){
                userQw.eq(LanUserDO::getWxName,req.getWxName());
            }
            List<LanUserDO> userDOList = lanUserMapper.selectList(userQw);
            if(ObjectUtil.isNotEmpty(userDOList)){
                Set<Long> userIdSet = userDOList.stream().map(LanUserDO::getUserId).collect(Collectors.toSet());
                userIds = userIdSet.stream().collect(Collectors.toList());
            }
        }
        return userIds;
    }

    private LanUserDO queryUserByUserId(Long userId){
        LambdaQueryWrapper<LanUserDO> userQw = new LambdaQueryWrapper<>();
        userQw.eq(LanUserDO::getUserId,userId);
        userQw.isNotNull(LanUserDO::getUserPhone);
        userQw.ne(LanUserDO::getUserPhone,"");
        return lanUserMapper.selectOne(userQw);
    }
}
